﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.DAL;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_MAS_BorrowTypes : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_MAS_BorrowTypes bt = new B_FIM_MAS_BorrowTypes();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //======= test only =========
                //Session["EmpSeq"] = "1";
                //===========================

                LoadDDLBgmSourceSeq();
                UpdateUI1("ViewAll");
            }
        }

        protected void LoadRadGrid1()
        {
            RadGrid1.DataSource = bt.GetFull(null);
        }

        protected IList<M_BGM_MAS_Source> LoadDDLFilterBgmSourceSeq()
        {
            return source.GetFull(true);
        }

        protected void LoadDDLBgmSourceSeq()
        {
            ddlBgmSourceSeq.DataSource = source.GetFull(true);
            ddlBgmSourceSeq.DataBind();
        }

        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll1.Visible = false;
                    cmdAddNew1.Visible = true;
                    cmdEdit1.Visible = true;
                    cmdDelete1.Visible = true;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = true;
                    PanelEdit1.Visible = false;
                    break;
                case "AddNew":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    break;
                case "Edit":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    break;
            }
        }

        protected void ClearControl1()
        {
            txtBorrowTypeName.Text = "";
            txtReturnDay.Text = "";
        }

        //============================ control events ==============================

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            //if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            //{
            //    e.Item.Selected = true;
            //    string strCurrencySeq = e.Item.OwnerTableView.Items[e.Item.ItemIndex]["currency_seq"].Text;
            //    Session["CurrencySeq"] = strCurrencySeq;
            //}
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                LoadRadGrid1();
            }
        }

        protected void cmdViewAll1_Click(object sender, EventArgs e)
        {
            UpdateUI1("ViewAll");
            ClearControl1();
        }

        protected void cmdAddNew1_Click(object sender, EventArgs e)
        {
            UpdateUI1("AddNew");
        }

        protected void cmdEdit1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                UpdateUI1("Edit");

                string strBorrowTypeSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["borrow_type_seq"].Text;
                string strBorrowTypeName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["borrow_type_name"].Text;
                string strBgmSourceSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bgm_source_seq"].Text;
                string strCalDateType = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["cal_date_type"].Text;
                string strReturnDay = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["return_day"].Text;
                string strIsTravel = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["is_travel"].Text;

                lblBorrowTypeSeq.Text = main.WebString(strBorrowTypeSeq);
                txtBorrowTypeName.Text = main.WebString(strBorrowTypeName);
                ddlBgmSourceSeq.SelectedValue = main.WebString(strBgmSourceSeq);
                rblCalDateType.SelectedValue = main.WebString(strCalDateType);
                txtReturnDay.Text = main.WebString(strReturnDay);
                chkIsTravel.Checked = strIsTravel == "1";
            }
            else
            {
                main.MessageAlert(Page, "กรุณาเลือกรายการที่ต้องการแก้ไข");
            }
        }

        protected void cmdDelete1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                M_FIM_MAS_BorrowTypes info = new M_FIM_MAS_BorrowTypes();
                string strUserID = Session["EmpSeq"].ToString();
                DateTime dtNow = DateTime.Now;
                string strBorrowTypeSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["borrow_type_seq"].Text;

                info.borrow_type_seq = Convert.ToInt32(strBorrowTypeSeq);
                info.updated_by = main.ConvertNullInt(strUserID);
                info.updated_when = dtNow;

                bt.Delete(info);

                LoadRadGrid1();
                RadGrid1.DataBind();
            }
        }

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
            M_FIM_MAS_BorrowTypes info = new M_FIM_MAS_BorrowTypes();
            string strBorrowTypeName = txtBorrowTypeName.Text;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            string strCalDateType = rblCalDateType.SelectedValue;
            string strReturnDay = txtReturnDay.Text;
            string strIsTravel = (chkIsTravel.Checked ? "1" : "0");
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.borrow_type_name = strBorrowTypeName;
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.cal_date_type = strCalDateType;
            info.return_day = main.ConvertNullInt(strReturnDay);
            info.is_travel = strIsTravel;
            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = dtNow;

            bt.Insert(info);

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Insert");
            ClearControl1();
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {
            M_FIM_MAS_BorrowTypes info = new M_FIM_MAS_BorrowTypes();
            string strBorrowTypeSeq = lblBorrowTypeSeq.Text;
            string strBorrowTypeName = txtBorrowTypeName.Text;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            string strCalDateType = rblCalDateType.SelectedValue;
            string strReturnDay = txtReturnDay.Text;
            string strIsTravel = (chkIsTravel.Checked ? "1" : "0");
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.borrow_type_seq = Convert.ToInt32(strBorrowTypeSeq);
            info.borrow_type_name = strBorrowTypeName;
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.cal_date_type = strCalDateType;
            info.return_day = main.ConvertNullInt(strReturnDay);
            info.is_travel = strIsTravel;
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = dtNow;

            bt.Update(info);

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Update");
            ClearControl1();
        }

        //==========================================================================
    }
}
